package com.example.idachuappone.order;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.idachuappone.R;
import com.example.idachuappone.order.frag.FragOrderAll;
import com.example.idachuappone.order.frag.FragOrderComment;
import com.example.idachuappone.order.frag.FragOrderPay;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class OrderFragTwo extends Fragment implements OnClickListener {
	private Button btn_all;
	public Button btn_comment;
	public Button btn_pay;
	public FragOrderAll fragOrderAll;
	public FragOrderComment fragOrderComment;
	public FragOrderPay fragOrderPay;
	public int index;
	private List<Fragment> lists;
	private int loginRequest;
	private OrderAdapter orderAdapter;
	private ViewPager viewPager;

	private class OrderAdapter extends FragmentStatePagerAdapter {
		private List<Fragment> list;

		public OrderAdapter(FragmentManager fm) {
			super(fm);
		}

		public void setList(List<Fragment> list) {
			this.list = list;
		}

		@Override
		public Fragment getItem(int arg0) {
			return this.list == null ? null : (Fragment) this.list.get(arg0);
		}

		@Override
		public int getCount() {
			return this.list == null ? 0 : this.list.size();
		}
	}

	public OrderFragTwo() {
		this.loginRequest = 255;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.order_two, null);
		initView(view);
		if (PrefUtil.getInstance(getActivity()).getUID() != null) {
			initData();
		}
		return view;
	}

	private void initData() {
		this.lists = new ArrayList();
		this.fragOrderAll = new FragOrderAll();
		this.fragOrderPay = new FragOrderPay();
		this.fragOrderComment = new FragOrderComment();
		this.lists.add(this.fragOrderAll);
		this.lists.add(this.fragOrderPay);
		this.lists.add(this.fragOrderComment);
		this.orderAdapter.setList(this.lists);
		this.orderAdapter.notifyDataSetChanged();
		this.viewPager.setCurrentItem(0);
	}

	private void initView(View view) {
		this.btn_all = (Button) view.findViewById(R.id.btn_all);
		this.btn_all.setOnClickListener(this);
		this.index = 1;
		this.btn_pay = (Button) view.findViewById(R.id.btn_pay);
		this.btn_pay.setOnClickListener(this);
		this.btn_comment = (Button) view.findViewById(R.id.btn_comment);
		this.btn_comment.setOnClickListener(this);
		initButtonLine(this.index);
		this.viewPager = (ViewPager) view.findViewById(R.id.vp_order);
		this.viewPager.setOffscreenPageLimit(BitmapCacheManagementTask.MESSAGE_CLOSE);
		this.orderAdapter = new OrderAdapter(getChildFragmentManager());
		this.viewPager.setAdapter(this.orderAdapter);
		this.viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				if (arg0 == 0) {
					OrderFragTwo.this.initButtonLine(1);
				} else if (arg0 == 1) {
					OrderFragTwo.this.initButtonLine(BitmapCacheManagementTask.MESSAGE_FLUSH);
				} else {
					OrderFragTwo.this.initButtonLine(BitmapCacheManagementTask.MESSAGE_CLOSE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_comment /* 2131165370 */:
			this.index = 3;
			this.viewPager.setCurrentItem(BitmapCacheManagementTask.MESSAGE_FLUSH, false);
		case R.id.btn_all /* 2131165486 */:
			this.index = 1;
			this.viewPager.setCurrentItem(0, false);
		case R.id.btn_pay /* 2131165487 */:
			this.index = 2;
			this.viewPager.setCurrentItem(1, false);
		default:
		}
	}

	public void initButtonLine(int index) {
		this.btn_all.setTextColor(getResources().getColor(R.color.color_two_title_txt));
		this.btn_pay.setTextColor(getResources().getColor(R.color.color_two_title_txt));
		this.btn_comment.setTextColor(getResources().getColor(R.color.color_two_title_txt));
		this.btn_all.setBackgroundResource(R.drawable.img_order_left);
		this.btn_pay.setBackgroundResource(R.drawable.img_order_middle);
		this.btn_comment.setBackgroundResource(R.drawable.img_order_right);
		if (index == 1) {
			this.btn_all.setTextColor(getResources().getColor(R.color.white));
			this.btn_all.setBackgroundResource(R.drawable.img_order_left_p);
		} else if (index == 2) {
			this.btn_pay.setTextColor(getResources().getColor(R.color.white));
			this.btn_pay.setBackgroundResource(R.drawable.img_order_middle_p);
		} else if (index == 3) {
			this.btn_comment.setTextColor(getResources().getColor(R.color.white));
			this.btn_comment.setBackgroundResource(R.drawable.img_order_right_p);
		}
	}
}
